package org.project.s.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Details2;

import org.project.pojo.GoodsDetail;
import org.project.pojo.Recorddetails;
import org.project.pojo.Show1;
import org.project.service.user.ImgsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Controller
@RequestMapping("/Functionlist")
public class Functionlist {
	
@Resource
private ImgsService imgsService;
	
	@RequestMapping("/listfunction1")
	public String listfunction11(@Param("functionNum")String functionNum){
		 HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
   	    
		   Recorddetails recorddetails=imgsService.selectdetails(functionNum);
		 
		   
		   List<Details2> list=imgsService.selectdetails2(functionNum);
		   List<Details2>  list2=new ArrayList<Details2> ();
		   if(null!=list){
			   
			   Iterator<Details2> it = list.iterator();
			   int flag=0;
				while(it.hasNext()) {
					Details2 details2=it.next();
				//	System.out.println(details2);
					details2.setFlag(flag);
					list2.add(details2);
					flag++;
								
				}
		   }
		   
		 req.setAttribute("list2", list2);      
		 req.setAttribute("recorddetails", recorddetails);
		     
		
		return "functionlist";
	}
	
	
	//根据功能编号查找到整行信息，发送到修改页面中
	@RequestMapping("/editfunction1")
	public String listfunction1(@Param("functionNum")String functionNum){
		 HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
   	    
		   Recorddetails recorddetails=imgsService.selectdetails(functionNum);
	
	
		      
		 req.setAttribute("recorddetails", recorddetails);
		     
		
	     	return "xiugaifirst";
	}
	

	
	//修改上面部分
	@RequestMapping("/editfunction2")
	public String listfunction1(Recorddetails recorddetails){
		
		int i=imgsService.editfunction2(recorddetails);
		int j=imgsService.editfunction3(recorddetails);
		if(i>0&&j>0){
			
			return "mainfra";
		}else{
			return "xiugaifirst";
		}
	}
	
	
	//根据明细的id 查找到整行数据，发送到修改明细页面中
   @RequestMapping("/editdetails2")
	public String editdetaillist(@Param("id")int id){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   Details2 details2=imgsService.editdetaillist11(id);
	   req.setAttribute("details2", details2);
	   
	   return "xiugaidetails"; 
   }
	
   
   
   
   @RequestMapping("/editdetaillist22")
   public String editdetaillist22(Details2 details2){
	  /* HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
   
	   int prepayPrice=details2.getPrepayPrice();
	   int id=details2.getId();
	   System.out.println(prepayPrice);
	   int prepay2=Integer.valueOf(prepayPrice);*/
	   
	  
	   int i=imgsService.editdetaillist2(details2);
	   if(i>0){
		   return "mainfra";
	   }
	      return "xiugaidetails";
	   
   }	
   
   
   @RequestMapping("/deletedetaillist")
   public String deletedetaillist(@Param("id")int id){
	  
	   int i=imgsService.deletedetails(id);
	   if(i>0){
		   return "mainfra";
	   }else{
		   return "xiugaidetails";
	   }
   }

   
   
   @RequestMapping("/adddetailslist")
   public String adddetailslist(GoodsDetail goodsDetail ){
	   int i=imgsService.adddetails(goodsDetail);
	 
	   if(i>0){
		   return "mainfra";
	   }else{
		   return "adddetailslist";
	   }
   }
   
   
   @RequestMapping("/add1")
    public String add1(@Param("functionNum")int functionNum){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   req.setAttribute("functionNum", functionNum);
	   return "adddetailslist";  
   }
   
   
   @RequestMapping("/selectintroduce")
   public String selectintroduce(@Param("functionNum")String functionNum){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   Show1 show1=imgsService.select1(functionNum);
	   req.setAttribute("show1", show1);
	   
	   return "introduce";
	   
   }
   
   @RequestMapping("/selectitemDetails")
   public String selectitemDetails(@Param("functionNum")String functionNum){
	 
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   Show1 show1=imgsService.select1(functionNum);
	   req.setAttribute("show1", show1);
	   
	   return "itemDetails";
	   
   }
   
   @RequestMapping("/updateintroduce")
   public String updateintroduce(@Param("functionNum")String functionNum,@Param("introduce")String introduce){
	   System.out.println(functionNum);
	   System.out.println(introduce);
	   Show1 show1=new Show1();
	   show1.setFunctionNum(functionNum);
	   show1.setIntroduce(introduce);
	   int i=imgsService.updateintroduce(show1);
	   System.out.println(i);
	   if(i>0){
		   return "mainfra";
	   }else{
		   return "mainfra";
	   }
	   
   }
   
   
   @RequestMapping("/updateitemDetails")
   public String updateitemDetails(@Param("functionNum")String functionNum,@Param("itemDetails")String itemDetails){
	  System.out.println(functionNum);
	   System.out.println(itemDetails);
	   Show1 show1=new Show1();
	   show1.setFunctionNum(functionNum);
	   show1.setItemDetails(itemDetails);
	   int i=imgsService.updateitenDetails(show1);
	   if(i>0){
		   return "mainfra";
	   }else{
		   return "mainfra";
	   }
	   
   }
   
   
   @RequestMapping("/selectintroduce2")
   public String selectintroduce2(@Param("functionNum")String functionNum){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   Show1 show1=imgsService.select1(functionNum);
	   req.setAttribute("show1", show1);
	   
	   return "editintroduce";
	   
   }
   
   
   
   @RequestMapping("/selectitemDetails2")
   public String selectitemDetails2(@Param("functionNum")String functionNum){
	 
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	   Show1 show1=imgsService.select1(functionNum);
	   req.setAttribute("show1", show1);
	   
	   return "edititemDetails";
   
   
   
}
}